<style lang="less">
    @import './login.less';
</style>

<template>
    <div class="login" @keydown.enter="handleSubmit">
        <div class="login-con">
            <Card :bordered="false">
                <p slot="title">
                    <Icon type="log-in"></Icon>
                    欢迎登录
                </p>
                <div class="form-con">
                    <Form ref="loginForm" :model="form" :rules="rules">
                        <FormItem prop="login_id">
                            <Input v-model="form.login_id" placeholder="请输入用户名">
                                <span slot="prepend">
                                    <Icon :size="16" type="person"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem prop="password">
                            <Input type="password" v-model="form.password" placeholder="请输入密码">
                                <span slot="prepend">
                                    <Icon :size="14" type="locked"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem>
                            <Button @click="handleSubmit" type="primary" long>登录</Button>
                        </FormItem>
                    </Form>
                </div>
            </Card>
        </div>
    </div>
</template>

<script>
import Cookies from 'js-cookie';
import axios from 'axios';
import themeSwitchVue from './main-components/theme-switch/theme-switch.vue';
export default {
    data () {
        return {
            form: {
                login_id: '',
                password: ''
            },
            rules: {
                login_id: [
                    { required: true, message: '账号不能为空', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '密码不能为空', trigger: 'blur' }
                ]
            }
        };
    },
    methods: {
        handleSubmit () {
            this.$refs.loginForm.validate((valid) => {
                if (valid) {
                    console.log('valid success');
                    axios({
                        method: 'post',
                        url: '/login',
                        data: {
                            login_id: this.form.login_id,
                            password: this.form.password
                        }
                    }).then((response) => {
                        console.log(response.data);
                        if(response.data.errorCode == 200){
                            //登录成功
                            Cookies.set('user', response.data.college_name);
                            this.$router.push({
                                name: 'home_index'
                            });
                        } else {
                            //alert('账户或密码错误');
                            this.$Notice.error({
                                title: '登录失败',
                                desc: response.data.errorMsg
                            });
                        }
                    }).catch(function (error) {
                        if (error.response) {
                        // 请求已发出，但服务器响应的状态码不在 2xx 范围内
                        console.log(error.response.data);
                        console.log(error.response.status);
                        console.log(error.response.headers);
                        } else {
                        // Something happened in setting up the request that triggered an Error
                        console.log('Error', error.message);
                        }
                        console.log(error.config);
                    });
                }
            });
        }
    }
};
</script>

<style>

</style>
